package org.thanos.demo.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {

    private static final String CHARACTER = ",";

    @Value("${spring.redisson.url}")
    private String url;

    @Value("${spring.redisson.cluster}")
    private Boolean cluster;

    @Bean(name = "redissonClient")
    public RedissonClient redissonClient() {
        Config config = new Config();
        if (cluster) {
            config.useClusterServers().addNodeAddress(url.split(CHARACTER));
        } else {
            config.useSingleServer().setAddress(url);
        }
        return Redisson.create(config);
    }
}
